package com.cee.admin.modules.sys.controller;

import com.cee.admin.common.BaseController;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.sys.entity.RoleMenu;
import com.cee.domain.modules.sys.vo.RoleMenuVo;
import com.cee.ds.modules.sys.service.IRoleMenuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * ϵͳ 前端控制器
 * </p>
 *
 * @author zcj
 * @since 2020-12-07
 */
@Tag(name = "系统角色组权限对应表")
@RestController
@RequestMapping("/api/v1/sys/role-menu")
public class RoleMenuController extends BaseController {

    @Autowired
    private IRoleMenuService iRoleMenuService;

    /*
     * 获取角色已分配的菜单数据
     * */
    @Operation(summary = "获取角色已分配的菜单数据")
    @GetMapping("getCheckedList")
    @RequiresPermissions(value = "sys:role-menu:select")
    public Result<List<RoleMenu>> getCheckedList(Long roleId) {
        try {
            return success(iRoleMenuService.list(new RoleMenu().setRoleId(roleId)));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }
    }

    /*
     * 分配角色菜单权限
     * */
    @Operation(summary = "分配角色菜单权限")
    @PostMapping("saveOrDelete")
    @RequiresPermissions(value = "sys:role-menu:write")
    public Result<Boolean> saveOrDelete(@RequestBody RoleMenuVo roleMenuVo) {
        return success(iRoleMenuService.saveRoleMenuVo(roleMenuVo));
    }

}

